<?php

/**
 * 	Media stream Object
 *
 * 	Outputs one or multiple streams 
 * 
 * 	Management is done by the 'group' streams in the engine..
 * 	This 'group' enables the user to add / edit / delete external videos (youtube / vimeo)
 * 	More information you can find in the request/media.php file
 *
 *	@copyright		oneDot only <we@onedotonly.be>
 *	@author			Pieter Van Peteghe <pieter@onedotonly.be>
 *	@create			18 june 2010
 */
 

class MediaStream extends MOD {

#	constructor	
	function __construct( $vars ) {
		$this->_addvars( $vars );
		$this->_ref(); 
	
	}
	
	
#	define output
	function output () {
		global $DB;
		
		$this->addclass( $this->action );
		
		$this->group = ( $this->vars->type )?
			$DB->reference( $this->vars->type, $this->vars->id, $this->vars->objtype ) : $DB->filter_direct( $this->vars->objtype );

		if( $this->action )
			$result = $this->{ "filter_" . $this->action }();
		 

		if( $result )
			return $this->parse( is_array( $result )?
				"<ul>" . implode( "\n", $result ) . "</ul>" :  $result
			
			) . $this->render_js();
	}
	
	
	// list of movies
	private function filter_stream_group() {
		global $DB;
	
		//return 'foo';
		$nodes = $DB->reference( 'group.stream', (int) $this->vars->id, 'media.stream' );
		$condt = 'ID in ('. (( count($nodes) == 1 )? $nodes[0]: implode(',',$nodes)) .') AND active ORDER BY rank DESC' . (( $this->vars->limit )? ' LIMIT '.$this->vars->limit : '' );
		$nodes = $DB->filter_objects( 'media.stream', $condt);
		
				
		// make list
		$return[]	= '<ul id="movies" class="movies">';			
		foreach ( $nodes as $key => $movie ) {
		
			$return[] = '<li class="movie">';
			$return[] = '<p class="streamTitle">' . $movie->get('name') . '</p>';
			$return[] = '<div style="position: relative; background: url(/public.img?id=' .$movie->get('ID') . '&sze=' . $this->vars->width . '.' . $this->vars->height . ') no-repeat top center; 
				width:' . $this->vars->width . 'px; height:' . $this->vars->height . 'px;" class="thumb">';
			$return[] = '<div class="playIt" style="width:' . $this->vars->width . 'px; height:' . $this->vars->height . 'px;" id="' . $movie->get('ID') . '"></div>';
			$return[] = '</div>';
			$return[] = '<div style="position: relative; width:' . $this->vars->width . 'px; height:' . $this->vars->height . 'px;" class="stream hide">';
			$return[] = $this->render_movie( $movie->get('id'), $movie->get('source'), $this->vars->width, $this->vars->height);
			$return[] = '</div>';
			$return[] = '</li>';
		
		}
		$return[] = '</ul>';
		
		$this->js = "
			mod = new MediaStream();
			mod.prepstream();
		";
		
		return implode( "\n", $return );
	}	
	
	
#	ajax filler
	function ajax_fill() {
		global $DB;
		
		$movie 	= $DB->filter_objects( 'media.stream', 'ID = ' . $this->vars->id );
		$res	= $this->render_movie( $movie[0]->values['meta']->id, $movie[0]->values['meta']->source, $this->vars->vars->refwidth, $this->vars->vars->refheight );
		
		return $res;
		
	}	
	
	function shuffle_assoc($input_array){

   		foreach($input_array as $key => $value)
     		$temp_array[$value][key] = $key;

  		shuffle($input_array);

   		foreach($input_array as $key => $value)
     		$return_array[$temp_array[$value][key]] = $value;
     
  	 	return $return_array;
   	}	
   	
   	
	function filter_single( $id ) {
		global $DB;
				
		$movie = array_pop( $DB->filter_objects( 'media.stream', 'ID = ' . $id ));
		$return .= '<p class="streamTitle">' . $movie->get('name') . '</p>';
		$return .= '<div style="position: relative; background: url(/public.img?id=' .$movie->get('ID') . '&sze=' . $this->vars->width . '.' . $this->vars->height . ') no-repeat top center; 
				width:' . $this->vars->width . 'px; height:' . $this->vars->height . 'px;" class="thumb">';
		$return .= '<div class="playIt" style="width:' . $this->vars->width . 'px; height:' . $this->vars->height . 'px;" id="' . $movie->get('ID') . '"></div>';
		$return .= '</div>';
		$return .= '<div style="position: relative; width:' . $this->vars->width . 'px; height:' . $this->vars->height . 'px;" class="stream hide">';
		$return .= $this->render_movie( $movie->get('id'), $movie->get('source'), $this->vars->width, $this->vars->height);
		$return .= '</div>';
		
		if( $this->vars->vars->objtypes || $this->vars->objtypes ){
			if( $mother = $DB->reference( (( $this->vars->objtypes )? $this->vars->objtypes: $this->vars->vars->objtypes ), false, 'media.stream', (int) $id )) {
				$mother = array_pop( $mother );
				$Mother = new OBJECT( (int) $mother[1], (int) $mother[0] );
				//print_r( $Mother );
				$return .= '<div class="mother">';
				$return .= '<span class="mother_type">' . $DB->type_r( $Mother->get( 'type' )) . '</span>';
				$return .= '<a href="' . $Mother->uri() . '" title="' . $Mother->get('name') . '">'  . $Mother->get('name') . '</a></div>';
				//echo $Mother->uri();
			}
		}

		return $return;
	}
	
	
	function filter_slideshow() {
		global $DB;
		
		// define group, streams
		if( $this->vars->type == 'group' ) 
			$nodes = $DB->reference( 'group.stream', (int) $this->vars->id, 'media.stream' );
			
		else if( $this->vars->type == 'recent' )	
			$nodes = $DB->filter_objects( 'media.stream', 'active AND nameid != "..." ORDER BY ID DESC LIMIT ' . $this->vars->limit );
		
		// movies	
		$i = 0;
		$return[]	= '<ul id="movies" class="movies">';
		foreach ( $this->shuffle_assoc( $nodes ) as $node ){
			$return[] = '<li class="movie ' . (( $i == 0) ? 'active': 'hide' ) . '" id="ref' . $i . '">';
			$movie = $this->filter_single( $node );
			$return[] = $movie;
			$return[] = '</li>';
			
			$i++;
		}
		$return[] = '</ul>';
			
		// navigation
		$return[] = '<div class="navigation">';
		for( $i = 0; $i< count( $nodes); $i++ ) 
			$return[] = '<a href="#" onclick="return false;" class="navigate' . (( $i == 0 ) ? ' active' : '' ) . '" rel="' . $i . '">next</a>';
		
		$return[] = '</div>';
			
		$this->js = "
			mod = new MediaStream();
			mod.slideshow();
			mod.prepstream();
		";
		return implode( "\n", $return );
		
	}
	
	
#	filter stream.group (like news)
	private function filter_groups() {
		global $DB;
		
		// make groups
		$groups = $DB->filter_objects( 'group.stream', ( $limit = $this->vars->limit )? "active order by rank desc, ID desc limit $limit" : "active" );
		
		if( $groups ) {
			
			foreach ($groups as $group) {
				
				// return in list
				$res[] = (( $group->get('name') != '...' ) ? '<li class="group ' . (( $this->vars->expand == 'all') ? " active" : "") . '"><span class="title">' . $group->get('name') . '</span>' : '');
				
				// return subs in list
				$streams = $DB->reference( 'group.stream', 'parentid = ' . $group->id, 'media.stream' );
				$res[] = '<ul>';
				foreach ($streams as $stream) {
					
					// get streams by parent
					$stream = $DB->filter_objects( 'media.stream', 'ID = ' . $stream );
					
					foreach ( $stream as $str ) {
					
						$res[] = '<li class="image"><img id="' . $str->id . '" class="image" src="/public.img?id=' .$str->id . '&sze=' . $this->vars->width . '.' . $this->vars->height . '&unt=true" /></li>';
						//print_r( $str );
					
					}
						
				}
				$res[] = '</ul>';
				$res[] = '</li>';
			
			}
			
		}
		
		if ($this->vars->ref ) {
			$script = "
				mod = new MediaStream(". json_encode( $this->vars ) .");
				mod.dominate('" . $this->vars->ref . "');
			";
			if		( $this->vars->expand == "first" )	$script .= "mod.expand('first');";
			
			$this->js = $script;

		}

		return $res;
	}	
	
	
#	filter social streams (think kriski)	
	private function filter_social_stream_group() {
		global $DB;
		
		$nodes = $DB->filter_objects( 'social.stream', 'active' . (( $this->vars->personal && $_SESSION['uid'] ) ? ' AND creator = ' . $_SESSION['uid'] : '') . ' ORDER BY ID desc' . (( $lim = $this->vars->limit ) ? ' LIMIT ' . $lim : '' ));
		
		$return[]	= '<ul id="movies" class="movies">';			
		foreach ( $nodes as $key => $movie ) {
		
			$return[] = '<li class="movie">';
			$return[] = '<div style="position: relative; width:' . $this->vars->width . 'px; height:' . $this->vars->height . 'px;" class="stream">';
			$return[] = $this->render_movie( $movie->get('id'), $movie->get('source'), $this->vars->width, $this->vars->height);
			$return[] = '</div>';
			$return[] = '</li>';
		
		}
		$return[] = '</ul>';
		
		return implode( "\n", $return );
	}
#	different parsers

	// output image
	function filter_single_image( $id ) {

		$id = (( $this->vars->id ) ? $this->vars->id : $id);
		return '<img class="image" src="/public.img?id=' .$this->vars->id . '&sze=' . $this->vars->width . '.' . $this->vars->height . '" />';
	
	}
	
	function filter_recent() {
		global $DB, $Object;
		
		$nodes = $DB->filter_objects( 'media.stream', "active order by rank desc, ID desc limit {$this->vars->limit}" );
		
		if( $Object ) {	
			$nodes	= array();
			if( $refs	= $DB->reference( $_GET['type'], (int) $_GET['id'], 'media.stream' )) {
				$refs	= array_slice( $refs, 0, $this->vars->limit );
				foreach( $refs as $ref ) 
					$nodes[] = new OBJECT( 'media.stream', $ref );
			}
		}
		
		if( $nodes )
			$return[]	= '<ul id="movies" class="movies">';
		
		$i = 0;			
		foreach ( $nodes as $key => $movie ) {
		
			//print_r( $movie );
			$source = $movie->get( 'source' );
			if( $movie->values['meta']->type )
				$source = $movie->values['meta']->type;
		
			$return[] = '<li class="movie" id="ref' . $i . '">';
			$return[] = '<p class="streamTitle">' . $movie->get('name') . '</p>';
			$return[] = '<div style="position: relative; background: url(/public.img?id=' . $movie->get('ID') . '&sze=' . $this->vars->width . '.' . $this->vars->height . ') no-repeat top center; 
				width:' . $this->vars->width . 'px; height:' . $this->vars->height . 'px;" class="thumb">';
			$return[] = '<div class="playIt" style="width:' . $this->vars->width . 'px; height:' . $this->vars->height . 'px;" id="' . $movie->get('ID') . '"></div>';
			$return[] = '</div>';
			$return[] = '<div style="position: relative; width:' . $this->vars->width . 'px; height:' . $this->vars->height . 'px;" class="stream hide">';
			$return[] = $this->render_movie( $movie->get('id'), $source, $this->vars->width, $this->vars->height);
			$return[] = '</div>';
			$return[] = '</li>';
		
			$i++;
		}

		if( $nodes )
			$return[] = '</ul>';
		
		$this->js = "
			mod = new MediaStream();
			mod.prepstream();
		";
		
		if( $nodes )
			return implode( "\n", $return );
		
	}
	
	
	// output image with link
	function filter_single_link() {
		global $DB;
		
		// get movie
		$movie 	= (object) $DB->get($this->mod, "ID = {$this->vars->id}");
		
		// get link
		$link	= $this->render_link( json_decode( $movie->meta )->id, json_decode( $movie->meta )->source );
		
		$return[]	= '<a target="_blank" href="' . $link . '">';
		$return[]	= '<img class="image" src="/public.img?id=' .$this->vars->id . '&sze=' . $this->vars->width . '.' . $this->vars->height . '" />';
		$return[]	= '</a>';
		
		return $return;
	}
	
	
	// output list of images with links
	function filter_links() {
		global $DB;
			
		$images	= $DB->filter($this->mod);
		
		// make list
		$return[]	= '<ul class="links">';			
		foreach ($images as $image) {
		
			$link	= $this->render_link( json_decode( $image['meta'] )->id, json_decode( $image['meta'] )->source );
			
			$return[] = '<li class="link">';
			$return[] = '<a target="_blank" href="' . $link . '">';
			$return[] = '<img src="/public.img?id=' .$image['ID'] . '&sze=' . $this->vars->width . '.' . $this->vars->height . '" />';
			$return[] = '</a></li>';
		
		}
		$return[] = '</ul>';
		
		return implode( "\n", $return );
	}


	// output list of images (all movies)
	function filter_images() {
		global $DB, $Object;
			
		$images	= $DB->filter( 'media.stream', $this->vars->limit? "active order by rank desc, ID desc limit {$this->vars->limit}" : 'active' );
		if( $Object ){
			$images = array();
			if( $refs	= $DB->reference( $_GET['type'], (int) $_GET['id'], 'media.stream' )) {
				$images = $DB->filter( 'media.stream', 'ID IN (' . implode( ', ', $refs ) . ') AND ' . (( $this->vars->limit )? "active order by rank desc, ID desc limit {$this->vars->limit}" : 'active' ));
			}
		}
		
		foreach ( $images as $image) {
			if ($image['name'] != '...' ) {
				$return[] = '<li class="image">';
				
				if( $this->vars->uri )
					$return[] = '<a href="' . engine_get( 'url' ) . '/' . $this->vars->uri . '" title="' . $image['name'] . '">';
				
				$return[] = '<img alt="' . $image["name"] . '" id="' . $image['ID'] . '" src="/public.img?id=' .$image['ID'] . '&sze=' . $this->vars->width . '.' . $this->vars->height . '" />';

				if( $this->vars->uri )
					$return[] = '</a>';

				$return[] = '</li>';
			}
		}
		
		if ($this->vars->ref ) {
			$this->js = "
				mod = new MediaStream(". json_encode( $this->vars ) .");
				mod.dominate('" . $this->vars->ref . "');
			";
		}
		
		if( $images )
			return $return;
	}


	// output single movie
	function filter_single_movie() {
		global $DB;
		
		// get movie
		$movie = (object) $DB->get($this->mod, "ID = {$this->vars->id}");
		
		// render
		return $this->render_movie( json_decode( $movie->meta )->id, json_decode( $movie->meta )->source, $this->vars->width, $this->vars->height );
	
	}


	// output list of movies
	function filter_movies() {
		global $DB;
		
		// get movies
		$movies	= $DB->filter($this->mod);
		
		// make list
		$return[]	= '<ul class="movies">';			
		foreach ($movies as $movie) {
			
			// render
			$return[] = '<li class="movie">' . $this->render_movie( json_decode( $movie['meta'] )->id, json_decode( $movie['meta'] )->source, $this->vars->width, $this->vars->height ) . '</li>';
		}
		$return[] = '</ul>';
		
		return $return;
	
	}



#	extra methods for media.stream output
	// get link of movie
	function render_link( $id, $source ) {
	
		if ($source == 'youtube') {
			$url	= 'http://www.youtube.com/v/' . $id . '&hl=en_US&fs=1&';
		} else if ($source == 'vimeo') {
			$url	= 'http://vimeo.com/moogaloop.swf?clip_id=' . $id . '&server=vimeo.com&show_title=1&show_byline=1&show_portrait=0&color=&fullscreen=1"';
		} else {
			$url	= 'http://www.youtube.com/v/' . $id . '&hl=en_US&fs=1&';
		}
		
		return $url;
	}
	
	
	// render movie
	function render_movie( $id, $source = 'youtube', $width = 480, $height = 385 ) {
	
		$url	= $this->render_link( $id, $source );
		
		$movie	= '<object width="' . $width . '" height="' . $height . '"><param name="movie" value="' . $url . '"></param>';
		$movie	.= '<param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param>';
		$movie	.= '<embed src="' . $url . '" type="application/x-shockwave-flash" 
			allowscriptaccess="always" allowfullscreen="true" width="' . $width . '" height="' . $height . '"></embed></object>';
			
		return $movie;
	}

}
 